package com.huanxing.cloud.miniapp.alipay.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huanxing.cloud.common.core.desensitization.KeyDesensitization;
import com.huanxing.cloud.common.core.entity.Result;
import com.huanxing.cloud.common.log.annotation.SysLog;
import com.huanxing.cloud.miniapp.alipay.service.IAliAppService;
import com.huanxing.cloud.miniapp.common.entity.AliApp;
import com.huanxing.cloud.security.annotation.HxInner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 支付宝应用
 *
 * @author lijx
 * @since 2022/3/14 15:36
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/aliapp")
@Api(value = "aliapp", tags = "支付宝应用")
public class AliAppController {

	private final IAliAppService aliAppService;

	private final KeyDesensitization keyDesensitization = new KeyDesensitization();

	@ApiOperation(value = "支付宝应用列表")
	@SaCheckPermission("alipay:alipayapp:page")
	@GetMapping("/page")
	public Result page(Page page, AliApp aliApp) {
		return Result.success(aliAppService.aliAppPage(page, aliApp));
	}

	@ApiOperation(value = "支付宝应用查询")
	@SaCheckPermission("alipay:alipayapp:page")
	@GetMapping("/{appId}")
	public Result getById(@PathVariable("appId") String appId) {
		return Result.success(aliAppService.getById(appId));
	}

	@HxInner
	@ApiOperation(value = "通过id查询支付宝应用")
	@GetMapping("/inner/{appId}")
	public Result getInnerById(@PathVariable("appId") String appId) {
		return Result.success(aliAppService.findByAppId(appId));
	}

	@SysLog("新增支付宝应用")
	@ApiOperation(value = "支付宝应用新增")
	@SaCheckPermission("alipay:alipayapp:add")
	@PostMapping
	public Result add(@RequestBody AliApp aliApp) {
		return Result.success(aliAppService.save(aliApp));
	}

	@SysLog("修改支付宝应用")
	@ApiOperation(value = "支付宝应用修改")
	@SaCheckPermission("alipay:alipayapp:edit")
	@PutMapping
	public Result edit(@RequestBody AliApp aliApp) {
		if (StrUtil.isEmpty(aliApp.getAppId())) {
			return Result.fail("appId is empty");
		}
		if (StrUtil.isEmpty(aliApp.getPrivateKey())) {
			return Result.fail("privateKey is empty");
		}
		AliApp target = aliAppService.getById(aliApp.getAppId());
		if (ObjectUtil.isNull(target)) {
			return Result.fail("aliApp is null");
		}
		if (keyDesensitization.serialize(target.getPrivateKey()).equals(aliApp.getPrivateKey())) {
			aliApp.setPrivateKey(null);
		}
		if (keyDesensitization.serialize(target.getEncryptKey()).equals(aliApp.getEncryptKey())) {
			aliApp.setEncryptKey(null);
		}
		return Result.success(aliAppService.updateById(aliApp));
	}

	@SysLog("删除支付宝应用")
	@ApiOperation(value = "支付宝应用删除")
	@SaCheckPermission("alipay:alipayapp:del")
	@DeleteMapping("/{id}")
	public Result del(@PathVariable String id) {
		return Result.success(aliAppService.removeById(id));
	}

}
